#include "Include.h"
#include "Settings.h"
#include "Data.h"
#include "ScalingWorker.h"

#include "StorageInterface.h"



int ACE_TMAIN (int argc, ACE_TCHAR * argv[])
{
	ACE_Process_Mutex mu(SCALER_NAME);
	if( mu.tryacquire() != 0)
	{
		printf("You can't load multiple scaler in one machine!\n");
		return -1;
	}

	if(!Settings::Start())
	{
		return -1;
	}

	LOG::Start(); 

	if(Settings::GetSettings()->LOCAL_ADDR.length() == 0)
	{
		printf("[scaler] can not get local addr\n");
		return -1;
	}


	if(!Settings::GetSettings()->IsControllerAddr())
	{
		printf("[scaler] Invalid scaler for this zone\n");
		return -1;
	}


	printf("\n#################### First Setting Values ################\n");
	printf("%s" , Settings::GetSettings()->dump().c_str());
	printf("\n##########################################################\n");
	printf("LOCAL ADDR : %s\n" , Settings::GetSettings()->LOCAL_ADDR.c_str());
	printf("###############################################################\n");


	LOG::PushProcessLog(_REQUIRED , "START");
	printf("[scaler] started\n");

	PROCESS cp;
	cp.activate();

	ScalingWorker w;
	w.DoWork();


	LOG::PushProcessLog(_REQUIRED , "END");
	printf("[scaler] terminated\n");


	Settings::Release();
	DBInterface::DeleteAllConnections();


	return 0;
}